import { _decorator, Component, Node, log, screen, v3, view, UITransform, Enum } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 适配屏幕高度的情况下适用，
 * 拉伸（可以选择维持宽高比）至全屏幕大小，以解决窄于屏幕壁纸的两侧黑边，也可使壁纸跨越安全区外
 */

export enum ResizeMode {
    Stretch,
    FixAspect
}
Enum(ResizeMode);

const _v30 = v3();

@ccclass('RescaleToVisibleSize')
export class RescaleToVisibleSize extends Component {

    @property({ type: ResizeMode })
    resizeMode: ResizeMode = ResizeMode.Stretch;

    start() {
        const size = view.getVisibleSize();
        const designSize = view.getDesignResolutionSize();

        let k = size.width / designSize.width;

        if (this.resizeMode == ResizeMode.Stretch) {
            if (k > 1) { // 宽较大，如果是背景图片的话，需要放大
                // this.node.getComponent(UITransform).width *= k;
                this.node.getScale(_v30);
                _v30.x *= k;
                this.node.setScale(_v30);
            }
        } else if (this.resizeMode == ResizeMode.FixAspect) {
            if (k > 1) { // 宽较大，如果是背景图片的话，需要放大
                this.node.getScale(_v30);
                _v30.multiplyScalar(k);
                this.node.setScale(_v30);
            }
        }
    }
}


